package org.hxyjs.service.impl;

import org.hxyjs.dao.GameDetailsRecordDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.GameDetailsRecord;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.GameDetailsRecordService;
import org.hxyjs.vo.GameDetailsRecordView;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service("gameDetailsRecordService")
public class GameDetailsRecordServiceImpl implements GameDetailsRecordService {
    @Resource
    GameDetailsRecordDao gameDetailsRecordDao;

    //查询全部
    @Override
    public DTO<GameDetailsRecord> getAllGameDetailsRecords()  throws Exception{
        List<GameDetailsRecord> gameDetailsRecords = gameDetailsRecordDao.getAllGameDetailsRecords();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(gameDetailsRecords).isPresent()?MessageEnum.GETGAMEDETAILSRECORDALLSUCCESS:MessageEnum.GETGAMEDETAILSRECORDALLFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage(),gameDetailsRecords);
    }

    //删除
    @Override
    public DTO<GameDetailsRecord> deleteGameDetailsRecordByGdrno(String gdrno)  throws Exception{
        Integer result = gameDetailsRecordDao.deleteGameDetailsRecordByGdrno(gdrno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEGAMEDETAILSRECORDSUCCESS:MessageEnum.DELETEPROGRAMMEFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    //按主键查询
    @Override
    public DTO<GameDetailsRecord> getGameDetailsRecordByGdrno(String gdrno)  throws Exception{
        GameDetailsRecord gameDetailsRecords = gameDetailsRecordDao.getGameDetailsRecordByGdrno(gdrno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(gameDetailsRecords).isPresent()?MessageEnum.GETGAMEDETAILSRECORDSUCCESS:MessageEnum.GETGAMEDETAILSRECORDFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage(),gameDetailsRecords);
    }

    //修改
    @Override
    public DTO<GameDetailsRecord> updateGameDetailsRecordByGdrno(GameDetailsRecordView gameDetailsRecordView) throws Exception {
        GameDetailsRecord gameDetailsRecord = dateFormat(gameDetailsRecordView);
        Integer result = gameDetailsRecordDao.updateGameDetailsRecordByGdrno(gameDetailsRecord);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEGAMEDETAILSRECORDSUCCESS:MessageEnum.UPDATEGAMEDETAILSRECORDFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    //增加
    @Override
    public DTO<GameDetailsRecord> addGameDetailsRecord(GameDetailsRecord gameDetailsRecord) throws Exception {
        Integer result = gameDetailsRecordDao.addGameDetailsRecord(gameDetailsRecord);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDGAMEDETAILSRECORDSUCCESS:MessageEnum.ADDGAMEDETAILSRECORDFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<GameDetailsRecord> getGameDetailsRecordByPage(PageInfo pageInfo ,String cacheId) throws Exception {
        List<GameDetailsRecord> gameDetailsRecords = gameDetailsRecordDao.getGameDetailsRecordByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(gameDetailsRecords).isPresent()?MessageEnum.GETGAMEDETAILSRECORDSUCCESS:MessageEnum.GETGAMEDETAILSRECORDFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage(),gameDetailsRecords,pageInfo);
    }

    public DTO<GameDetailsRecord> getGameDetailsRecordByPage(PageInfo pageInfo) throws Exception {
        List<GameDetailsRecord> gameDetailsRecords = gameDetailsRecordDao.getGameDetailsRecordByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(gameDetailsRecords).isPresent()?MessageEnum.GETGAMEDETAILSRECORDSUCCESS:MessageEnum.GETGAMEDETAILSRECORDFAIL;
        return new DTO<GameDetailsRecord>(messageEnum.getCode(),messageEnum.getMessage(),gameDetailsRecords,pageInfo);
    }

    public GameDetailsRecord dateFormat(GameDetailsRecordView gameDetailsRecordView) throws Exception{
        GameDetailsRecord gameDetailsRecord = new GameDetailsRecord();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        gameDetailsRecord.setGdrno(gameDetailsRecordView.getGdrno());
        gameDetailsRecord.setCrno(gameDetailsRecordView.getCrno());
        gameDetailsRecord.setGdrplayer(gameDetailsRecordView.getGdrplayer());
        gameDetailsRecord.setResult(gameDetailsRecordView.getResult());
        gameDetailsRecord.setGdrtime(sdf.parse(gameDetailsRecordView.getGdrtime()));
        return gameDetailsRecord;
    }

}
